<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: 牧羊人 <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\model;


use app\admin\service\UserRolePlatService;

/**
 * 后台渠道用户-模型
 * @author ZhangXuanNeng
 * @since: 2023/08/22
 * Class UserPlat
 * @package app\admin\model
 */
class UserPlat extends BaseModel
{
    // 设置数据表名
    protected $name = "user_channel";

    /**
     * 获取缓存信息
     * @param int $id 记录ID
     * @return \app\common\model\数据信息|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @author ZhangXuanNeng
     * @since: 2023/08/22
     */
    public function getInfo($id)
    {
        $info = parent::getInfo($id); // TODO: Change the autogenerated stub
        if ($info) {
                                                                
            // 头像
//            $info['avatar'] = get_image_url($info['avatar']);
            // 获取用户角色列表
            $userRoleService = new UserRolePlatService();
            $roleList = $userRoleService->getUserRoleList($id);
            $info['roles'] = $roleList;
        }
        return $info;
    }

    /**
     * 补全列表信息
     * @param $getList
     * @return mixed
     */
    public function index($getList){
        if ($getList){
            $userIdList = array_column($getList->toArray(), "id");
            //获取用户对应的角色
            $userRolePlatModel = new UserRolePlat();
            $roleInfo = $userRolePlatModel->alias("ur")
                ->field(['ur.user_id', 'r.id', 'r.name'])
                ->join(DB_PREFIX . 'role_channel r', 'ur.role_id=r.id')
                ->distinct(true)
                ->where('ur.user_id', 'in', $userIdList)
                ->where('r.status', '=', 1)
                ->order('r.sort asc')
                ->select()->toArray();
            $userRoleInfo = array();
            if (!empty($roleInfo)){
                foreach ($roleInfo as $roleItem){
                    $userRoleInfo[$roleItem['user_id']][] = array("id"=>$roleItem['id'], "name"=>$roleItem['name']);
                }
            }
            $List = [];
            foreach ($getList as $value){
                // 头像
                if ($value['avatar']) $value['avatar'] = get_image_url($value['avatar']);

                $result = array();
                $result['id'] = $value['id'];
                $result['avatar'] = $value['avatar'];
                $result['realname'] = $value['realname'];
                $result['nickname'] = $value['nickname'];
                $result['gender'] = $value['gender'];
                $result['username'] = $value['username'];
                $result['password'] = $value['password'];
                $result['first_channel'] = $value['first_channel'];
                $result['second_channel'] = $value['second_channel'];
                $result['pid'] = $value['pid'];
                $result['level'] = $value['level'];
                $result['package'] = $value['package'];
                $result['package_url'] = $value['package_url'];
                $result['mobile'] = $value['mobile'];
                $result['note'] = $value['note'];
                $result['create_time'] = $value['create_time'];
                $result['email'] = $value['email'];
                $result['address'] = $value['address'];
                $result['intro'] = $value['intro'];
                $result['first_commission'] = $value['first_commission'];
                $result['renewal_commission'] = $value['renewal_commission'];
                $result['merchantId'] = $value['merchantId'];
                $result['androidSdk'] = $value['androidSdk'];
                $result['iosSdk'] = $value['iosSdk'];
                $result['appKey'] = $value['appKey'];
                $result['appSecret'] = $value['appSecret'];
                $result['desKey'] = $value['desKey'];
                $result['status'] = $value['status'];
                $result['package_status'] = $value['package_status'];
                if (isset($userRoleInfo[$value['id']])){
                    $result['roles'] = $userRoleInfo[$value['id']];
                }

                $List[] = $result;
            }
        }
        return $List;
    }
}